package com.itheima.reggie.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.itheima.reggie.common.EmployeeHolder;
import com.itheima.reggie.damain.Employee;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import java.util.Date;
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {

    //插入操作。自动填充
    @Override
    public void insertFill(MetaObject metaObject) {
        metaObject.setValue("createTime", new Date());
        metaObject.setValue("updateTime", new Date());
        //从session中获取当前登录信息
//        Employee employee = (Employee) session.getAttribute("SESSION_EMPLOYEE");
        Employee employee = EmployeeHolder.get();
        if (employee != null){
            //利用线程思想获取信息
            metaObject.setValue("createUser", employee.getId());
            metaObject.setValue("updateUser", employee.getId());
        }
//        metaObject.setValue("createUser", 1L);
//        metaObject.setValue("updateUser", 1L);
    }
//修改操作，自动填充
    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateTime", new Date());
        //从session中获取当前登录用户的信息
//        Employee employee = (Employee) session.getAttribute("SESSION_EMPLOYEE");
        Employee employee = EmployeeHolder.get();
        if (employee != null){
            metaObject.setValue("updateUser", EmployeeHolder.get().getId());
        }
//        metaObject.setValue("updateUser", 1L);
    }

}
